/*
 *  dcc/dccmain.c
 *
 *  Copyright (C) 2009  Jeongho Nah
 *
 *
 */

/*
 * This file handles the main parts of compilation
 * version : 0.02.001
 * 
 * There are two major compiling paths. (Chosen by compile options. Default "SOFT")
 * 1. SOFT PART
 * It takes role of semantic analyzer and emits Dokdo Virtual Machine format.
 * 2. HARD PART
 * It emits machine-dependent assembly code. (Current supports include x86, MIPS)
 *
 * Jeongho Nah / njh1983@korea.com / October 2009
 *
 */

 /*
  * 2009-10-13
  * TO DO LIST
  *
  * 1. compiler option parser
  * 2. parser should get expanded source
  * 3. blabla..
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dccstdlib.h"
#include "dccstdtype.h"

/* These global vars are also used in internal functions : parser.c */
FILE *sourceFile, *expsourceFile, *astFile, *softFile, *hardFile; /* DO NOT CHANGE! */

int main(int argc, char *argv[])
{
	char fileName[256];
	Node *root;

	/* Printing start messages */
	printf(" === Staring DCC compiler... === \n\n");
	printf("DCC is a acronym of Dokdo C-language Compiler.\n");
	printf("Do you know  what Dokdo is? Shortly,\n");
	printf("Dokdo is a group of disputed islets \n");
	printf("in the East Sea administered by South Korea.\n");
	printf("Visit: http://dccproject.tistory.com for more infomations.\n");
	printf("Thanks.\n\n\n");

	/* Input from command line prompt */
	if (argc !=2) {
		printf("NOT ENOUGH ARGVs.\n");
		printf("Usage$ dcc sampledcc.c\n\n\n");
		exit(1);
	}
	/* README : please implement option parser. 2009-10-13 */

	/* File controls */
	strncpy(fileName, argv[1], 255); /* Be careful with BOF attack! */
	printf(" >> source: %s\n", fileName);

	if ((sourceFile = fopen(fileName, "r")) == NULL) {
		printf("fopen error %s\n", fileName);
		exit(1);
	}
	expsourceFile = fopen(strcat(strtok(fileName, "."), ".exp"), "w"); /* expanded source */
	astFile = fopen(strcat(strtok(fileName, "."), ".ast"), "w");
	softFile = fopen(strcat(strtok(fileName, "."), ".dvf"), "w"); /* Dokdo VM format */
	hardFile = fopen(strcat(strtok(fileName, "."), ".o"), "w"); /* ELF format */


	/* Main part, there are two path. 1.SOFT 2.HARD */

	/* Start of Preprocessor */
	printf(" === Staring Preprocessor... === \n");
	preprocessor(); /* get orginal source */

	/* Start of Parser */
	printf(" === Staring Parser... === \n");
	astRoot = parser(); /* get expanded source */
	printTree(astRoot, 0);
	
	/* SOFT PART */
	/* Start of ICG(Intermediate Code Generator), emit llvm with 0.02 */
	printf(" === Starting Intermediate Code Generator... ===\n");
	icodeGen(astRoot);
	/* Start of soft optimizer. Machine-independent */
	printf(" === Starting soft optimizer... ===\n");
	soptimizer1(softFile);
	/* Print SOFT part ending messages */
	printf(" === end of SOFT part(*.dvf) ===\n");


	/* HARD PART */
	/* Start of HCG(Hard Code Generator), emit X86 or MIPS assembly */
	printf(" === Starting Hard Code Generator... ===\n");
	hcodeGen(softFile, hardFile);
	/* Start of hard optimizer(post-pass), Machine-dependent */
	printf(" === Starting hard optimizer... ===\n");
	hoptimizer(hardFile);
	/* Start of linking */
	printf(" === Starting Liking... ===\n");
	linkerWrapper();

	/* Print ending messages */
	printf(" === end of DCC ===\n");

	fclose(hardFile);
	fclose(softFile);
	fclose(astFile);
	fclose(expsourceFile);
	fclose(sourceFile);

	return 0;
}